package com.mobi.Survey.video;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;

/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * VideoPlayer.java Custom Video View Creates and setups a custom video player
 * with circular progress bar showing buffering
 */
public abstract class VideoPlayer extends VideoView implements
		OnPreparedListener
{

	public VideoPlayer(Context context)
	{
		super(context);
		this.ctx = context;

		((Activity) context).getWindow().setFlags(
				android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
				android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

	}

	public static final byte TYPE_GENERIC = 2;
	protected String mMsgInit = "Initializing";
	private Context ctx;
	protected ProgressBar mProgressBar;
	protected TextView mProgressMessage;
	protected IVideoCallbacks ivc;

	/**
	 * 
	 */

	public void showProgress()
	{
		mProgressBar.setVisibility(View.VISIBLE);
	}

	/**
	 * 
	 */
	public void dismissTextMessage()
	{
		mProgressMessage.setVisibility(View.GONE);
	}

	/**
 * 
 */
	public void dismissProgress()
	{

		mProgressBar.setVisibility(View.GONE);
	}

	/**
	 * 
	 */

	public void setUpPlayer(String path)
	{
		try
		{
			setVideoURI(Uri.parse(path));
		} catch (Exception e)
		{
			setVideoPath(path);
		}

		final MediaController lMediaController = new MediaController(ctx);
		setMediaController(lMediaController);

		lMediaController.show(0);
		setOnPreparedListener(this);

		this.startPlaying();
	}

	/**
	 * 
	 */

	public void startPlaying()
	{
		requestFocus();
		start();

	}

	/**
	 * stop playing video
	 */
	public void stopPlaying()
	{
		try
		{
			((Activity) ctx)
					.getWindow()
					.clearFlags(
							android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
			stopPlayback();
			ivc = null;
		} catch (Exception e)
		{

		}

	}

	@Override
	public abstract void onPrepared(MediaPlayer arg0);

	public void showMessage()
	{
		mProgressBar.setVisibility(View.VISIBLE);
		mProgressBar.bringToFront();
	}

	public void setMessage(String msg)
	{
		mProgressMessage.setText(msg);
	}

	/**
	 * Setup necessary views like TextView to show status of VideoView,
	 * Progressbar
	 * 
	 * @param if player type 1 return Video View for Youtube else for generic
	 * 
	 *        video
	 * 
	 * @return Renderable Video View
	 * 
	 */

	public View setupView(int playerType)
	{
		switch (playerType)
		{
		case 1:

		{
			LinearLayout v = new LinearLayout(ctx);
			LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
			v.setLayoutParams(param);

			RelativeLayout.LayoutParams paramParent = new RelativeLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

			RelativeLayout parent = new RelativeLayout(ctx);
			parent.setBackgroundColor(Color.GRAY);
			parent.setLayoutParams(paramParent);
			v.addView(parent);
			RelativeLayout.LayoutParams paramVideoView = new RelativeLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

			this.setLayoutParams(paramVideoView);
			this.setId(3);

			parent.addView(this);

			LinearLayout ll = new LinearLayout(ctx);
			RelativeLayout.LayoutParams paramMsgs = new RelativeLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

			ll.setOrientation(LinearLayout.VERTICAL);

			paramMsgs.addRule(RelativeLayout.CENTER_IN_PARENT);
			parent.addView(ll);
			ll.setLayoutParams(paramMsgs);

			LinearLayout.LayoutParams paramPBar = new LinearLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			mProgressBar = new ProgressBar(ctx);
			mProgressBar.setIndeterminate(true);
			mProgressBar.setVisibility(View.VISIBLE);
			mProgressBar.setEnabled(true);
			mProgressBar.setLayoutParams(paramPBar);

			mProgressMessage = new TextView(ctx);
			mProgressMessage.setLayoutParams(paramMsgs);
			mProgressMessage.setTextColor(Color.LTGRAY);
			mProgressMessage.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
			mProgressMessage.bringToFront();
			mProgressMessage.setVisibility(View.VISIBLE);
			mProgressMessage.setText("Buffering");
			ll.addView(mProgressBar);
			ll.addView(mProgressMessage);
			return v;
		}

		case 2:
		{
			LinearLayout lLinLayout = new LinearLayout(ctx);
			lLinLayout.setId(1);
			lLinLayout.setOrientation(LinearLayout.VERTICAL);
			lLinLayout.setGravity(Gravity.CENTER);
			lLinLayout.setBackgroundColor(Color.BLACK);

			LayoutParams lLinLayoutParms = new LayoutParams(
					ViewGroup.LayoutParams.FILL_PARENT,
					ViewGroup.LayoutParams.FILL_PARENT);
			lLinLayout.setLayoutParams(lLinLayoutParms);

			RelativeLayout lRelLayout = new RelativeLayout(ctx);
			lRelLayout.setId(2);
			lRelLayout.setGravity(Gravity.CENTER);
			lRelLayout.setBackgroundColor(Color.BLACK);
			android.widget.RelativeLayout.LayoutParams lRelLayoutParms = new android.widget.RelativeLayout.LayoutParams(
					ViewGroup.LayoutParams.FILL_PARENT,
					ViewGroup.LayoutParams.FILL_PARENT);
			lRelLayout.setLayoutParams(lRelLayoutParms);
			lLinLayout.addView(lRelLayout);

			this.setId(3);
			android.widget.RelativeLayout.LayoutParams lVidViewLayoutParams = new android.widget.RelativeLayout.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
			lVidViewLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
			this.setLayoutParams(lVidViewLayoutParams);
			lRelLayout.addView(this);

			mProgressBar = new ProgressBar(ctx);
			mProgressBar.setIndeterminate(true);
			mProgressBar.setVisibility(View.VISIBLE);
			mProgressBar.setEnabled(true);
			mProgressBar.setId(4);
			android.widget.RelativeLayout.LayoutParams lProgressBarLayoutParms = new android.widget.RelativeLayout.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
			lProgressBarLayoutParms.addRule(RelativeLayout.CENTER_IN_PARENT);
			mProgressBar.setLayoutParams(lProgressBarLayoutParms);
			lRelLayout.addView(mProgressBar);

			mProgressMessage = new TextView(ctx);
			mProgressMessage.setId(5);

			android.widget.RelativeLayout.LayoutParams lProgressMsgLayoutParms = new android.widget.RelativeLayout.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
			lProgressMsgLayoutParms.addRule(RelativeLayout.CENTER_HORIZONTAL);
			lProgressMsgLayoutParms.addRule(RelativeLayout.BELOW, 4);
			mProgressMessage.setLayoutParams(lProgressMsgLayoutParms);
			mProgressMessage.setTextColor(Color.LTGRAY);
			mProgressMessage.setText("Buffering");
			mProgressMessage.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
			mProgressMessage.bringToFront();
			lRelLayout.addView(mProgressMessage);

			return lLinLayout;
		}
		}
		return null;
	}

	/**
	 * callback listener for video event like, prepare, error, end
	 * 
	 * @param ivc
	 */
	public void setVideoCallbackListener(IVideoCallbacks ivc)
	{
		this.ivc = ivc;
	}

	// @Override
	// public abstract boolean onError(MediaPlayer arg0, int arg1, int arg2);

}
